package test

import java.io.FileOutputStream

import org.apache.poi.hssf.usermodel.{HSSFCell, HSSFSheet, HSSFWorkbook}
import org.apache.poi.hssf.util.HSSFColor
import org.apache.poi.ss.usermodel.{BorderStyle, FillPatternType, HorizontalAlignment, VerticalAlignment}

object TestExcelStyle extends App {


  val wb = new HSSFWorkbook
  val outputStream = new FileOutputStream("C:\\Users\\iplas\\Desktop\\testExcelStyle.xls")
  private val sheet: HSSFSheet = wb.createSheet("sheet1")

  // 加粗字体
  private lazy val B_FONT = {
    val font = wb.createFont()
    font.setBold(true)
    font.setFontName("宋体")
    font.setFontHeightInPoints(11)
    font
  }

  // 普通字体
  private lazy val NORMAL_FONT = {
    val font = wb.createFont()
    font.setFontName("宋体")
    font.setFontHeightInPoints(11)
    font
  }

  // 居中样式
  private lazy val OCStyle = {
    val style = wb.createCellStyle
    style.setBorderBottom(BorderStyle.THIN); //下边框
    style.setBorderLeft(BorderStyle.THIN);//左边框
    style.setBorderTop(BorderStyle.THIN);//上边框
    style.setBorderRight(BorderStyle.THIN);//右边框
    style.setVerticalAlignment(VerticalAlignment.CENTER)//设置居中
    style.setAlignment(HorizontalAlignment.CENTER)//设置居中
//    style.setFillForegroundColor(new XSSFColor(Color.decode("#dce6f1")).getIndexed)
    val palette = wb.getCustomPalette
    val myColor = palette.findSimilarColor(220, 230, 220)
    style.setFillForegroundColor(myColor.getIndex)
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND)
    style.setFont(B_FONT)
    style
  }

  val cell1: HSSFCell = sheet.createRow(0).createCell(0)
  cell1.setCellValue("居中样式")
  cell1.setCellStyle(OCStyle)

  // 靠左样式
  private lazy val OLStyle = {
    val style = wb.createCellStyle
    style.setBorderBottom(BorderStyle.THIN); //下边框
    style.setBorderLeft(BorderStyle.THIN);//左边框
    style.setBorderTop(BorderStyle.THIN);//上边框
    style.setBorderRight(BorderStyle.THIN);//右边框
    style.setAlignment(HorizontalAlignment.LEFT)//设置靠左
    style.setFillForegroundColor(HSSFColor.PALE_BLUE.index)
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND)
    style.setFont(B_FONT)
    style
  }

  val cell2: HSSFCell = sheet.createRow(1).createCell(0)
  cell2.setCellValue("靠左样式")
  cell2.setCellStyle(OLStyle)

  // 靠右样式
  private lazy val RStyle = {
    val style = wb.createCellStyle
    style.setBorderBottom(BorderStyle.THIN); //下边框
    style.setBorderLeft(BorderStyle.THIN);//左边框
    style.setBorderTop(BorderStyle.THIN);//上边框
    style.setBorderRight(BorderStyle.THIN);//右边框
    style.setAlignment(HorizontalAlignment.RIGHT)//设置靠右
    style.setFillForegroundColor(HSSFColor.PALE_BLUE.index)
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND)
    style.setFont(NORMAL_FONT)
    style
  }

  val cell3: HSSFCell = sheet.createRow(2).createCell(0)
  cell3.setCellValue("靠右样式")
  cell3.setCellStyle(RStyle)

  // 普通样式
  private lazy val NORMAL_Style = {
    val style = wb.createCellStyle
    style.setBorderBottom(BorderStyle.THIN); //下边框
    style.setBorderLeft(BorderStyle.THIN);//左边框
    style.setBorderTop(BorderStyle.THIN);//上边框
    style.setBorderRight(BorderStyle.THIN);//右边框
    style.setVerticalAlignment(VerticalAlignment.CENTER)//设置居中
    style.setFont(NORMAL_FONT)
    style
  }

  val cell4: HSSFCell = sheet.createRow(3).createCell(0)
  cell4.setCellValue("普通样式")
  cell4.setCellStyle(NORMAL_Style)


  wb.write(outputStream)

  outputStream.flush
  outputStream.close

}
